<template>
  <div class="studentList">
    <!-- 操作表单 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button type="primary" @click="add">增加</el-button>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="formInline.username" placeholder="请输入用户名查询"></el-input>
      </el-form-item>
      <el-form-item label="用户组">
        <el-input v-model="formInline.groupName" placeholder="请输入用户组查询(改下拉框"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query">查询(还没有实现)</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userId" label="用户编号" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" align="center"></el-table-column>
      <el-table-column prop="group.groupName" label="用户组" align="center"></el-table-column>
      <el-table-column prop="nickname" label="用户昵称" align="center"></el-table-column>
      <el-table-column prop="userComment" label="备注" align="center"></el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" align="center" :formatter="formatTime"></el-table-column>
      <el-table-column prop="gmtModified" label="最后修改时间" align="center" :formatter="formatTime"></el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small"
                     @click="updateInfo(scope.row)">
            <el-icon>
              <edit/>
            </el-icon>
          </el-button>
          <el-button type="danger" size="small"
                     @click="deleteInfo(scope.row)">
            <el-icon>
              <delete/>
            </el-icon>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 page.pageNum-->
    <el-row style="margin-top: 10px">
      <el-col :offset="15" :span="6">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="page.pageNum"
          @current-change="changePage"
          :page-count="page.pages"/>
      </el-col>
    </el-row>

    <!-- 新增信息的表单 -->
    <el-dialog :title="state ? '新增用户' : '修改用户'" v-model="dialogFromVisible" width="500px">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.pwd" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户组" :label-width="formLabelWidth">
          <el-select v-model="form.group.groupId" placeholder="请选择用户组">
            <template v-for="(item, index) of groups" :key="index">
              <el-option :label="item.groupName" :value="item.groupId"></el-option>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="用户昵称" :label-width="formLabelWidth">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.userComment" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
<!--          <span>还没加数据校验</span>-->
          <el-button type="primary" @click="dialogFromVisible = false">取消</el-button>
          <el-button type="primary" @click="submit('form')">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
  // import { Edit, Delete} from "@element-plus/icons-vue"

  export default {
    name: "StudentList",
    components: {
      // Edit,
      // Delete,
    },
    data() {
      return {
        formInline: {
          username: '',
          groupName: '',
        },
        tableData: [],
        dialogFromVisible: false,
        state: true,
        noneForm: {
          userId: '',
          username: '',
          pwd: '',
          nickname: '',
          userComment: '',
          group: {
            groupId: '',
          }
        },
        form: {
          userId: '',
          username: '',
          pwd: '',
          nickname: '',
          userComment: '',
          group: {
            groupId: '',
          }
        },
        formLabelWidth: "80px",

        page: {
          size: 11,
          pageNum: 1,
          pages: 10,
        },
        groups: {},
      };
    },
    created() {
      this.changePage(1);
      this.service.get("/group")
        .then(res => {
          console.log(res)
          this.groups = res.data.data.groups;
        })
        .catch(err => {
          console.log(err)
        })
    },
    methods: {
      query() {
        console.log(this.formInline);
      },

      add() {
        if (!this.state) this.form = this.noneForm;
        this.state = true;
        this.dialogFromVisible = true;
      },
      updateInfo(row) {
        console.log(row, "updateInfo");
        this.state = false;
        this.dialogFromVisible = true;

        this.form.userId = row.userId;
        this.form.username = row.username;
        this.form.pwd = "";
        this.form.group.groupId = row.group.groupId;
        this.form.nickname = row.nickname;
        this.form.userComment = row.userComment;
      },
      deleteInfo(row) {
        this.service.delete("/user/" + row.userId)
          .then(res => {
            console.log(res);
            this.changePage(this.page.pageNum);
          })
          .catch(err => {
            console.log(err);
          });
      },

      submit() {
        if (this.form.pwd === "") this.form.pwd = null
        if (this.state) {
          this.service.post('/user', this.form)
            .then(res => {
              console.log(res);
              let data = res.data.data;
              this.tableData.splice(0, 0, data.user);
            })
            .catch(err => {
              console.log(err);
            })
        } else {
          console.log(this.form, "发送请求前的 this.form ")
          this.service.put('/user', this.form)
            .then(res => {
              console.log(res);
              let i;
              for (i = 0; i < this.tableData.length; i++) {
                if (this.tableData[i].userId === this.form.userId)
                  break;
              }
              this.tableData.splice(i, 1, res.data.data.user)
            })
            .catch(err => {
              console.log(err);
            })
        }
        this.dialogFromVisible = false;
      },
      formatTime(row, column, cellValue) {
        let time1 = cellValue.substring(0, 10);
        let time2 = cellValue.substring(11, 19);
        return time1 + ' ' + time2;
      },
      changePage(pageNum) {
        console.log("页码改变到第 " + pageNum + " 页");
        this.service.get('user/' + pageNum + '/' + this.page.size)
          .then(res => {
            console.log(res);
            let data = res.data.data;
            this.tableData = data.page.list;
            this.page.pageNum = data.page.pageNum;
            this.page.pages = data.page.pages;
          })
          .catch(err => {
            console.error(err);
          });
      },
    },
  }

</script>

<style scoped lang="scss">
  .studentList {
    .demo-form-inline, .el-form-item {
      text-align: left;
    }

    .el-select {
      width: 100%;
    }

    .dialog-footer {
      text-align: center;
    }
  }
</style>